import re, wx
from .synth import login, logout, Synthesizer
from .settings import getRules
class SynthManager:
    # 合成器管理器类
    def __init__(self):
        # 初始化合成器
        login()
        self.synthesizer = Synthesizer()
    def terminate(self):
        # 释放资源
        self.synthesizer.terminate()
        del self.synthesizer
        logout()
    def speak(self, text, callback = None, pitch = None):
        # 朗读函数，处理多语音规则逻辑
        rules = getRules()
        if not rules: # 没有任何规则就用默认参数合成
            self.synthesizer.speak(text, callback = callback, pitch = pitch)
            return
        regex = [f'(?P<{k}>{v["regex"]})' for k, v in rules.items() if v['regex']]
        if not regex: # 有规则，但是无效的情况
            self.synthesizer.speak(text, callback = callback, pitch = pitch)
            return
        # 创建正则的迭代器
        regexIter = re.finditer('|'.join(regex), text, flags = re.I)
        start = 0
        def cb():
            # 每当有一个文本匹配后，会通过这个函数处理
            nonlocal start
            key, msg, msg2 = None, None, None
            def cb2():
                rule = rules[key]
                if 'regex' in rule.keys():
                    del rule['regex']
                self.synthesizer.speak(msg2, callback = cb, **rule)
            try: # 得到迭代器中的一个匹配结果
                matched = next(regexIter)
                for k, v in matched.groupdict().items():
                    if not v:continue
                    i = matched.start()
                    msg = text[start:i]
                    key, msg2 = k, v
                    start = matched.end()
                    break
                # 别忘了每个匹配到的结果之间的部分也需要朗读
                if msg:
                    # 先朗读匹配结果之间的部分，朗读完毕回调cb2函数
                    self.synthesizer.speak(msg, callback = cb2, pitch = pitch)
                else:
                    cb2()
            except StopIteration: # 如果匹配到最后一个结果，别忘了后面的部分
                msg = text[start:]
                self.synthesizer.speak(msg, callback = callback, pitch = pitch)
        cb() # 首次手动驱动
    def pause(self,switch):
        # 暂停函数
        self.synthesizer.pause(switch)
    def stop(self):
        # 停止函数
        self.synthesizer.stop()
    def getSpeed(self):
        # 获取语速
        return self.synthesizer.getSpeed()
    def setSpeed(self, speed):
        # 设置语速
        return self.synthesizer.setSpeed(speed)
    def getSpeedIncrease(self):
        # 获取语速增强
        return self.synthesizer.getSpeedIncrease()
    def setSpeedIncrease(self, speedIncrease):
        # 设置语速增强
        return self.synthesizer.setSpeedIncrease(speedIncrease)
    def getSpeedIncreases(self):
        # 获取所有语速增强
        return self.synthesizer.getSpeedIncreases()
    def getTempo(self):
        # 获取播放倍速
        return self.synthesizer.getTempo()
    def setTempo(self, tempo):
        # 设置播放倍速
        return self.synthesizer.setTempo(tempo)
    def getTempos(self):
        # 获取所有播放倍速
        return self.synthesizer.getTempos()
    def getVoice(self):
        # 获取声音
        return self.synthesizer.getVoice()
    def setVoice(self, voice):
        # 设置声音
        return self.synthesizer.setVoice(voice)
    def getVoices(self):
        # 获取所有声音
        return self.synthesizer.getVoices()
    def getRdn(self):
        # 获取数字读法
        return self.synthesizer.getRdn()
    def setRdn(self, rdn):
        # 设置数字读法
        return self.synthesizer.setRdn(rdn)
    def getRdns(self):
        # 获取所有数字读法
        return self.synthesizer.getRdns()
    def getPitch(self):
        # 获取音高
        return self.synthesizer.getPitch()
    def setPitch(self, pitch):
        # 设置音高
        return self.synthesizer.setPitch(pitch)
    def getVolume(self):
        # 获取音量
        return self.synthesizer.getVolume()
    def setVolume(self, volume):
        # 设置音量
        return self.synthesizer.setVolume(volume)
